package com.gcsoftware.web.service;

import com.gcsoftware.utils.MapUtils;
import com.gcsoftware.web.mapper.T_GW_COMPANYMAPPER;
import com.gcsoftware.web.beans.T_GW_COMPANY;
import com.gcsoftware.utils.GenerateIDs;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by qs on 2018/3/14.
 */
@Service
public class T_GW_COMPANY_SERVICE {
    @Autowired
    private T_GW_COMPANYMAPPER mapper ;

    public List<T_GW_COMPANY> queryForList(String c_COMPANY_NAME){
        return queryForList(c_COMPANY_NAME , null);
    }

    public List<T_GW_COMPANY> queryForList(String c_COMPANY_NAME,Integer c_COMPANY_TYPE){
        return mapper.queryForList(c_COMPANY_NAME , c_COMPANY_TYPE);
    }

    public int getMaxId(){
        String id = mapper.getMaxId();
        int maxId = 0;
        if(StringUtils.isNotBlank(id)) {
            maxId = Integer.valueOf(id.replace(GenerateIDs.ID_TYPE_COMPANY_ID,""));
        }
        return maxId;
    }

    public T_GW_COMPANY get(String c_COMPANY_ID ,  String c_COMPANY_NAME) {
        return mapper.get(StringUtils.isBlank(c_COMPANY_ID)?null : c_COMPANY_ID,StringUtils.isBlank(c_COMPANY_NAME)?null : c_COMPANY_NAME);
    }

    public MapUtils insert(T_GW_COMPANY company){
        if (GenerateIDs.getMaxCompanyId() == 0) {
            GenerateIDs.setMaxCompanyId(getMaxId());
        }
        company.setC_COMPANY_ID(GenerateIDs.GenerateId(GenerateIDs.ID_TYPE_COMPANY_ID));

        if (null != get(company.getC_COMPANY_ID() , null)) {
            GenerateIDs.setMaxCompanyId(0);
            insert(company);
        }
        if (null != get(null ,company.getC_COMPANY_NAME())) {
            MapUtils.error("已存在同名公司");
        }
        if (1 == mapper.insert(company)) {
            /*if (StringUtils.isNotBlank(verifyId)) {
                T_GW_VERIFY verify = new T_GW_VERIFY();
                verify.setc_VERIFY_ID(verifyId);
                verify.setC_SUBMIT_USER(userId);
                verify.setC_VERIFY_STATUS(SysConst.VERIFY_STATUS_UNVERIFY);
                verify.setC_VERIFY_TYPE(SysConst.VERIFY_TYPE_COMPANY_REGISTER);
                verifyMapper.insertVerify(verify);
            }
            try {
                MailUtils.sendMail(mailConfigs.getMailSendFrom(),new String[]{mailConfigs.getAdminMail()},null,mailConfigs.getSubjectAccountReg(),"公司：" + company.getC_COMPANY_NAME() +"刚刚在数据安全解决方案系统注册了<br/>请及时审核！" ,true);
            } catch (Exception e) {
                e.printStackTrace();
            }*/
            return MapUtils.ok();
        } else {
            return MapUtils.error("新增公司失败");
        }
    }

    public int update(T_GW_COMPANY com)
    {
        return mapper.update(com);
    }
    public int updateStatus(String c_COMPANY_ID, String c_COMPANY_STATUS)
    {
        return mapper.updateStatus(c_COMPANY_ID,c_COMPANY_STATUS);
    }

    public int delete(String c_COMPANY_ID) {
        return mapper.delete(c_COMPANY_ID);
    }

}
